package io.vector.study.uploader.controller;

import io.vector.study.uploader.service.IUploadHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by vector on 2017/5/23.
 */
@RestController
@RequestMapping(value = "/uploads")
public class UploadController {

    private Logger logger = LoggerFactory.getLogger(getClass());

    private static final String UPLOAD_PATH = "C:\\Users\\vector\\Desktop\\upload";

    @Autowired
    private IUploadHandler uploadHandler;

    @PostMapping(value = "")
    public void saveFile(MultipartFile file) {
        uploadHandler.saveFile(file);
    }

    @PostMapping(value = "/chunks")
    public void saveFileChunk(MultipartFile file,
                       @RequestParam String fileId,
                       @RequestParam Integer chunks,
                       @RequestParam Integer chunk,
                       HttpServletRequest request, HttpServletResponse response) throws Exception {
        uploadHandler.saveFileChunk(fileId, file, chunks, chunk);
    }

    @PostMapping(value = "/chunks/merge/{fileId}")
    public void mergeFileChunks(@PathVariable String fileId) {
        uploadHandler.mergeFileChunks(fileId);
    }

}
